    //
//  DayDetailViewController.m
//  MonthView
//
//  Created by  ibokan on 10-9-14.
//  Copyright 2010 tencent.com. All rights reserved.
//

#import "DayDetailViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "NSDate-Helper.h"
#import "ArtEvent.h"
#import "ArtTour.h"
#import "ArtRoadshowTour.h"
#import "DetailWebViewController.h"
#import "CalendarViewController.h"
#import "SmartRoadShowAppDelegate.h"
#import "MyTabBarController.h"
#import "RS3ViewController.h"
#import "MyNavigationController.h"
#import "RS1ViewController.h"
#import "Reachability.h"

@implementation DayDetailViewController
@synthesize artEventArray,artTourArray,artRoadshowArray;
@synthesize calenderVC;

-(id) init 
{
	if(self = [super init])
	{
		UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 7, 100, 30)];
		titleLable.text = @"活动日历";
		titleLable.backgroundColor = [UIColor clearColor];
		titleLable.font = [UIFont systemFontOfSize:20];
		self.navigationItem.titleView = titleLable;
		[titleLable release];
	}
	
	return self;
}


-(void) loadView
{
	self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 345)];
	self.view.layer.contents = (id)[UIImage imageNamed:@"dayTableViewBG.png"].CGImage;
	
	sortTag = 1;
	
	selectDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 180, 25)];
	selectDateLabel.backgroundColor = [UIColor clearColor];
	selectDateLabel.font = [UIFont boldSystemFontOfSize:14];
	selectDateLabel.textAlignment = UITextAlignmentCenter;
	selectDateLabel.text = @"";
	[self.view addSubview:selectDateLabel];
	
	UIImageView *tipImageView = [[UIImageView alloc] initWithFrame:CGRectMake(55, 321, 6, 6)];
	tipImageView.image = [UIImage imageNamed:@"cal_point0.png"];
	tipImageView.backgroundColor = [UIColor clearColor];
	[self.view addSubview:tipImageView];
	[tipImageView release];
	
	UIImageView *tipImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(175, 321, 6, 6)];
	tipImageView1.image = [UIImage imageNamed:@"cal_point1.png"];
	tipImageView1.backgroundColor = [UIColor clearColor];
	[self.view addSubview:tipImageView1];
	[tipImageView1 release];
	
	UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(155, 315, 2, 18)];
	[line setImage:[UIImage imageNamed:@"line.png"]];
	[self.view addSubview:line];
	[line release];
	
	
	UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 312, 200, 25)];
	[tipLabel setBackgroundColor:[UIColor clearColor]];
	[tipLabel setFont:[UIFont systemFontOfSize:12]];
	[tipLabel setText:@"都市新主张                    艺术地图"];
	[tipLabel setTextColor:[UIColor grayColor]];
	[self.view addSubview:tipLabel];
	[tipLabel release];
	
	
	myTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 40, 300, 270)];
	myTableView.delegate = self;
	myTableView.dataSource = self;
	[myTableView setShowsVerticalScrollIndicator:NO];
	[myTableView setBackgroundColor:[UIColor clearColor]];
	[myTableView setSeparatorColor:[UIColor clearColor]];
	myTableView.layer.cornerRadius = 3;
	[self.view addSubview:myTableView];
	
	
}

//设置Label显示的时间
-(void) setSelectDate:(NSDate *)date
{
	int year = [date getYear];
	int month = [date getMonth];
	int day = [date getDay];
	
	NSString *str = [NSString stringWithFormat:@"%d年 %d月 %d日",year, month, day];
	selectDateLabel.text = str;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc 
{
	[myTableView release];
	[selectDateLabel release];
    [super dealloc];
}



#pragma mark UITableViewDelegate,dataSource
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
	if(section == 0)
	{
		return [artTourArray count];
	}
	else if(section == 1)
	{
		return [artEventArray count];
	}
//	else if(section == 2){
//		return [artRoadshowArray count];
//	}
	return 0;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//	static NSString *CellIdentify = @"dayDetailCell";
//	dayDetailTableViewCell  *cell = (dayDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentify];
	dayDetailTableViewCell *cell = nil;
	if(!cell)
	{
//		cell = [[[dayDetailTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentify] autorelease];
		cell = [[[dayDetailTableViewCell alloc] init]autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;

	}
	NSUInteger section = [indexPath section];
	NSUInteger row = [indexPath row];
	
	cell.indexPath = indexPath;
	cell.delegate = self;
	
	switch (section) {
		case 0:
		{
			ArtTour *artTour = [artTourArray objectAtIndex:row];
						
			[cell setCellStyle:2];
			cell.typeFlag = 1;
			[cell setImageWithURL:artTour._datepic];
			cell.sortLabel.text = @"都市新主张";
			cell.sortLabel.textColor =  [UIColor colorWithRed:0.46 green:0.62 blue:0 alpha:1.0];
			cell.infoLabel.text = [NSString stringWithFormat:@"%@站",artTour._city];
			[cell setTimeLabelText:artTour._starttime End:artTour._endtime];
			[cell setPushById:[NSString stringWithFormat:@"%d", artTour._id] City:artTour._city Time:[NSString stringWithFormat:@"%@,%@", artTour._starttime, artTour._endtime] Title:@""];			
			break;
		}
		case 1:
		{
			ArtEvent *artEvent1 = [artEventArray objectAtIndex:row];
					
			[cell setCellStyle:0];
			cell.typeFlag = 2;
			[cell setImageWithURL:artEvent1._thumb];
			cell.sortLabel.text = @"艺术地图";
			cell.sortLabel.textColor = [UIColor colorWithRed:1 green:0.45 blue:0 alpha:1.0];
			cell.infoLabel.text = [NSString stringWithFormat:@"%@站-%@",artEvent1._city, artEvent1._title];
			[cell setTimeLabelText:artEvent1._starttime End:artEvent1._endtime];
			[cell setPushById:[NSString stringWithFormat:@"%d", artEvent1.news_id] City:artEvent1._city Time:[NSString stringWithFormat:@"%@,%@", artEvent1._starttime, artEvent1._endtime] Title:artEvent1._title];			
			break;
		}
		default:
			break;
	}

	
	return cell;
	
}

#pragma mark dayDetailTableViewCell delegate
- (void)seeDetailInfoForIndexPath:(NSIndexPath *)indexPath{
	
	switch (indexPath.section) {
		case 0:{
			SmartRoadShowAppDelegate *appDelegate = (SmartRoadShowAppDelegate *)[[UIApplication sharedApplication] delegate];
			MyTabBarController *myTabBarController = appDelegate.tabBarController;
			[myTabBarController selectControllerAtIndex:0];
			
			RS3ViewController* rs3VC = (RS3ViewController*)[((MyNavigationController*)[myTabBarController.viewControllers objectAtIndex:0]) topViewController];
			[rs3VC.segmentControl setSelectedSegmentIndex:0];
			[rs3VC segmentedControl:rs3VC.segmentControl segmentDidChangeToIndex:0];
			
			ArtTour *artTour = [artTourArray objectAtIndex:indexPath.row];
			NSLog(@"tour id = %d",artTour._id);
			
			[appDelegate.rs3VC ScrollViewScrollToCityIndex:artTour._id - 1];
			break;
	    }
		case 1:{	
			
			Reachability *internetReach = [Reachability reachabilityForInternetConnection];
			[internetReach startNotifier];	
			NetworkStatus netStatus = [internetReach currentReachabilityStatus];
			
			if (netStatus) {
				
				DetailWebViewController *detailVC = [[DetailWebViewController alloc] init];
				ArtEvent *artEvent1 = [artEventArray objectAtIndex:indexPath.row];
				NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:artEvent1._content]];
				[detailVC.webView loadRequest:request];
				[calenderVC.navigationController pushViewController:detailVC animated:YES];
				[detailVC release];
			}
			else {
				UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"本功能需要网络支持，请检查网络连接！"
															   delegate:nil cancelButtonTitle:@"确定"
													  otherButtonTitles:nil];
				[alert show];
				[alert release];		
			}
			
			
			break;
		}
		default:
			break;
	}
	

	
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//	
//	DetailWebViewController *detailVC = [[DetailWebViewController alloc] init];
//
//	switch (indexPath.section) {
//		case 0:{
//			NSString *htmlString = [NSString stringWithFormat:@"%@",[[artTourArray objectAtIndex:indexPath.row] _description]];
//			[detailVC.webView loadHTMLString:htmlString baseURL:nil];
//			break;
//		}
//		case 1:{
//			ArtEvent *artEvent1 = [artEventArray objectAtIndex:indexPath.row];
//			NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:artEvent1._content]];
//			[detailVC.webView loadRequest:request];
//			break;
//		}
//		default:
//			break;
//	}
//	
//	[calenderVC.navigationController pushViewController:detailVC animated:YES];
//	[detailVC release];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 132;
}



@end
